Grokking Amazon Coding Interview
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
Amazon Easy Problems
Redistribute Characters to Make All Strings Equal (easy)
Solution: Redistribute Characters to Make All Strings Equal
Longest Common Prefix (easy)
Solution: Longest Common Prefix
Make Array Zero by Subtracting Equal Amounts (easy)
Solution: Make Array Zero by Subtracting Equal Amounts
Leaf-Similar Trees (easy)
Solution: Leaf-Similar Trees
Path Crossing (easy)
Solution: Path Crossing
Special Array With X Elements Greater Than or Equal X (easy)
Solution: Special Array With X Elements Greater Than or Equal X
Relative Sort Array (easy)
Solution: Relative Sort Array
Transpose Matrix (easy)
Solution: Transpose Matrix
Find Common Characters (easy)
Solution: Find Common Characters
Fizz Buzz (easy)
Solution: Fizz Buzz
Increasing Order Search Tree (easy)
Solution: Increasing Order Search Tree
Largest Odd Number in String (easy)
Solution: Largest Odd Number in String
Best Time to Buy and Sell (easy)
Solution: Best Time to Buy and Sell Stock
Take Gifts From the Richest Pile(easy)
Solution: Take Gifts From the Richest Pile
Remove Duplicates from Sorted List (easy)
Solution: Remove Duplicates from Sorted List
Maximum Length of Subarray With Positive Product (medium)
Solution: Maximum Length of Subarray With Positive Product
Largest Submatrix With Rearrangements (medium)
Solution: Largest Submatrix With Rearrangements
Count the Number of Good Subarrays (medium)
Solution: Count the Number of Good Subarrays
Minimum Suffix Flips (medium)
Solution: Minimum Suffix Flips
Minimum Number of Operations to Make Array Empty (medium)
Solution: Minimum Number of Operations to Make Array Empty
Next Greater Element II (medium)
Solution: Next Greater Element II
Make Sum Divisible by P (medium)
Solution: Make Sum Divisible by P
Rotting Oranges (medium)
Solution: Rotting Oranges
Minimum Deletions to Make Character Frequencies Unique (medium)
Solution: Minimum Deletions to Make Character Frequencies Unique
Minimum Adjacent Swaps to Make a Valid Array (medium)
Solution: Minimum Adjacent Swaps to Make a Valid Array
Reverse Integer (medium)
Solution: Reverse Integer
Maximize Distance to Closest Person (medium)
Solution: Maximize Distance to Closest Person
Split Linked List in Parts (medium)
Solution: Split Linked List in Parts
Maximum Difference Between Node and Ancestor (medium)
Solution: Maximum Difference Between Node and Ancestor
Deepest Leaves Sum (medium)
Solution: Deepest Leaves Sum
The kth Factor of n (medium)
Solution: The kth Factor of n
Coin Change II (medium)
Solution: Coin Change II
Sum of Subarray Minimums (medium)
Solution: Sum of Subarray Minimums
Valid Sudoku (medium)
Solution: Valid Sudoku
Longest Increasing Subsequence (medium)
Solution: Longest Increasing Subsequence
Next Permutation (medium)
Solution: Next Permutation
Subsets (medium)
Solution: Subsets
Combination Sum (medium)
Solution: Combination Sum
Number of Provinces (medium)
Solution: Number of Provinces
Number of Islands (medium)
Solution: Number of Islands
Constrained Subsequence Sum (hard)
Solution: Constrained Subsequence Sum
Number of Valid Subarrays (hard)
Solution: Number of Valid Subarrays
Count Vowels Permutation (hard)
Solution: Count Vowels Permutation
Concatenated Words (hard)
Solution: Concatenated Words
Minimum Replacements to Sort the Array (hard)
Solution: Minimum Replacements to Sort the Array
Count All Valid Pickup and Delivery Options (hard)
Solution: Count All Valid Pickup and Delivery Options
Length of the Longest Valid Substring (hard)
Solution: Length of the Longest Valid Substring
Count Array Pairs Divisible by K (hard)
Solution: Count Array Pairs Divisible by K
Word Ladder (hard)
Solution: Word Ladder
Path with Maximum Sum (hard)
Solution: Path with Maximum Sum
Solution: Take Gifts From the Richest Pile

Problem Statement

You're presented with several piles of gifts, with each pile containing a certain number of gifts. Every second, you'll engage in the following activity:

  • Pick the pile that contains the highest number of gifts. If multiple piles share this distinction, you can select any of them.
  • Compute the square root of the number of gifts in the selected pile, and then leave behind that many gifts (rounded down). Take all the other gifts from this pile.
  • You'll do this for "k" seconds. The objective is to find out how many gifts would still remain after these "k" seconds.

Examples

    • Input: gifts = [4, 9, 16], k = 2
    • Expected Output: 11
    • Justification:
      1. Take from third pile (16 gifts): leave ( ) = 4 gifts, take 12. Remaining gifts = [4, 9, 4]
      2. Take from second pile (9 gifts): leave ( ) = 3 gifts, take 6. Remaining gifts = [4, 3, 4]
    • Input: gifts = [1, 2, 3], k = 1
    • Expected Output: 4
    • Justification:
      1. Take from third pile (3 gifts): leave ( ) = 1 gift (rounded down), take 2. Remaining gifts = [1, 2, 1]
    • Input: gifts = [25, 36, 49], k = 3
    • Expected Output: 18
    • Justification:
      1. Take from third pile (49 gifts): leave ( ) = 7 gifts, take 42. Remaining gifts = [25, 36, 7]
      2. Take from second pile (36 gifts): leave ( ) = 6 gifts, take 30. Remaining gifts = [25, 6, 7]
      3. Take from first pile (25 gifts): leave ( ) = 5 gifts, take 20. Remaining gifts = [5, 6, 7]

Constraints:

  • 1 <= gifts.length <= 103
  • 1 <= gifts[i] <= 109
  • 1 <= k <= 103

Solution

In order to solve this problem, we leverage a max heap data structure. A max heap allows us to efficiently retrieve and process the pile with the most gifts at any given time. Once we choose the pile with the maximum number of gifts, we compute the number of gifts we'd leave behind (which is the floor value of the square root of the gifts in that pile) and then push this new value back into the max heap. This operation is repeated 'k' times. After these 'k' operations, we sum up the remaining gifts in all the piles to get our final answer.

Detailed Explanation:

  1. Heap Initialization: We start by creating a max heap. However, some programming languages, like Python, provide a min heap by default. To turn this into a max heap, we can insert negative values. By doing so, retrieving the smallest element from this heap effectively gives us the pile with the most gifts.

  2. Processing 'k' Operations: For each of the 'k' operations, we:

    • Pop the heap to get the pile with the most gifts.
    • Calculate the gifts we'll take by subtracting the floor value of the square root of the current pile size.
    • Push the remaining gifts back into the max heap.
  3. Calculating Remaining Gifts: After the 'k' operations are completed, we simply sum up all the elements present in our heap. This total represents the number of gifts left across all piles.

  4. Returning the Result: Finally, the accumulated result is returned as the answer.

This algorithm efficiently processes the piles and allows us to always operate on the pile with the most gifts, ensuring that the gifts are taken in the most optimal manner as per the problem's requirements. The use of a max heap provides a clear way to consistently choose the correct pile to operate on without having to sort or iterate through the entire array in each of the 'k' operations.

Algorithm Walkthrough:

Using the input gifts = [4, 9, 16], k = 2:

  • Convert gifts to a max-heap: [16, 9, 4]
  • 1st second:
    • Pop 16. Calculate ( ) = 4. Push back 4 in the max-heap.
    • New heap: [9, 4, 4]
  • 2nd second:
    • Pop 9. Calculate ( ) = 3. Push back 3 to the max-heap.
    • New heap: [4, 4, 3]
  • Remaining gifts in the heap: [4, 4, 3]

Thus, the total remaining gifts = 4 + 4 + 3 = 11.

Code

Here is the code for this algorithm:

Java
Saved to Cloud

. . . .

Time Complexity:

  1. Heapification: Converting the given gifts list to a heap is O(n*log(n)) where n is the length of the gifts array. Although in a strict sense, building a heap from an unordered array is O(n), in our solution we perform an O(n*log(n)) operation since we're continuously pushing items to the heap.

  2. K Operations: For each of the k operations, we:

    • Pop an item from the heap, which is O(log(n)).
    • Push the reduced item back to the heap, which is again O(log(n)).

    Thus, for k operations, this part of the algorithm runs in O(k*log(n)).

  3. Final Summation: After the k operations, summing up the remaining gifts in the heap takes linear time O(n).

Combining these, our total time complexity is: [ O(nlog(n) + klog(n) + n) ] For large values of n and k, this approximates to: [ O(nlog(n) + klog(n)) ]

Space Complexity:

  1. Heap Space: We use a max-heap to store the gifts which takes O(n) space.

  2. Constant Space: Other variables and counters used in the algorithm take constant space, O(1).

Hence, the total space complexity is: [ O(n) ]

Take Gifts From the Richest Pile(easy)
Remove Duplicates from Sorted List (easy)
Mark as Completed

On this page

Problem Statement

Examples

Solution

Algorithm Walkthrough:

Code

Time Complexity:

Space Complexity: